package Skill;

import java.io.IOException;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import Access_check.Access_checkMapper;
import Board.BoardMapper;
import Content.ContentItem;
import Content.ContentMapper;
import Content_check.Content_checkMapper;
import Contest.ContestMapper;
import Contest_reg.Contest_regMapper;
import Contest_reg_file.Contest_reg_fileMapper;
import Member.MemberItem;
import Member.MemberMapper;
import Proposal.ProposalItem;
import Proposal.ProposalMapper;
import Reply.ReplyMapper;
import SkillTree.SkillTreeMapper;
import Subject.SubjectItem;
import Subject.SubjectMapper;
import Team.TeamItem;
import Team.TeamMapper;
import TeamSkill.TeamSkillMapper;
import Track.TrackMapper;
import User.UserMapper;



@Controller
public class SkillController {
	@Autowired
	private SkillMapper skillMapper;
	
	@Autowired
	private SkillTreeMapper skillTreeMapper;
	
	@Autowired
	private TeamSkillMapper teamSkillMapper;
	
	@Autowired
	private TeamMapper teamMapper;
	
	@RequestMapping("/common/skillTreeList.do")
	public @ResponseBody List<HashMap<String,Object>> skillTreeList(){
		return skillTreeMapper.selectAll();
	}
	
	@RequestMapping("/common/skillList.do")
	public @ResponseBody List<HashMap<String,Object>> skillList(int skillTree_id){
		return skillMapper.selectById(skillTree_id);
	}
	
	@RequestMapping("/common/skillDelete.do")
	public @ResponseBody boolean skillDelete(int skill_id){
		if(teamSkillMapper.selectBySkillId(skill_id)==0){
			
			skillMapper.delete(skill_id);
			return true;
		}
		else{
			return false;
		}
	}
	
	@RequestMapping("/common/skillTreeDelete.do")
	public @ResponseBody boolean skillTreeDelete(int skillTree_id){
		if(skillMapper.selectById(skillTree_id).isEmpty()){
			skillTreeMapper.delete(skillTree_id);
			return true;
		}
		else{
			return false;
		}
	}
	
	@RequestMapping("/common/skillAdd.do")
	public @ResponseBody HashMap<String,Object> skillAdd(String skill_name,int skillTree_id){
		
		if(skillMapper.selectByName(skill_name)==null){
			HashMap<String,Object> skill = new HashMap<String,Object>();
			skill.put("skill_name", skill_name);
			skill.put("skillTree_id", skillTree_id);
			skillMapper.insert(skill);
			return skillMapper.selectId(Integer.parseInt(skill.get("skill_id").toString()));
		}
		else{
			return null;
		}
	}
	
	@RequestMapping("/common/skillTreeAdd.do")
	public @ResponseBody HashMap<String,Object> skillTreeAdd(String skillTree_name){
		
		
		if(skillTreeMapper.selectByName(skillTree_name)==null){
			HashMap<String,Object> skillTree = new HashMap<String,Object>();
			skillTree.put("skillTree_name", skillTree_name);
			skillTreeMapper.insert(skillTree);
			return skillTreeMapper.selectById(Integer.parseInt(skillTree.get("skillTree_id").toString()));
		}
		else{
			return null;
		}
	}
	
	
	@RequestMapping("/common/skillModify.do")
	public @ResponseBody HashMap<String,Object> skillModify(String skill_name,int skill_id){
		
		if(skillMapper.selectByName(skill_name)==null){
			HashMap<String,Object> skill=new HashMap<String,Object>();
			skill.put("skill_id", skill_id);
			skill.put("skill_name", skill_name);
			skillMapper.update(skill);
			return skill;
		}
		else{
			return null;
		}
	}
	
	@RequestMapping("/common/skillTreeModify.do")
	public @ResponseBody HashMap<String,Object> skillTreeModify(String skillTree_name,int skillTree_id){
		
		if(skillTreeMapper.selectByName(skillTree_name)==null){
			HashMap<String,Object> skillTree=new HashMap<String,Object>();
			skillTree.put("skillTree_id", skillTree_id);
			skillTree.put("skillTree_name", skillTree_name);
			skillTreeMapper.update(skillTree);
			return skillTree;
		}
		else{
			return null;
		}
	}
	
}